package Dao;

import Model.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

public class DaoStuHandle {
    /**
     * 配置文件封装
     * @throws IOException
     */
    public static void info() throws IOException {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/config/log4j.properties");
        properties.load(fileInputStream);
        PropertyConfigurator.configure(properties);
    }

    public void showStuDAO() throws Exception {
        DaoStuHandle.info();
        InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis_config.xml");

        //获取一个工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //通过工厂获取一个对象
        SqlSession sqlSession = build.openSession();
        List<Student> students = sqlSession.selectList("StudentMapper.findAll");
        for (Student stu:students){
            System.out.println("信息：" + stu);
        }

    }


    public void addStuDAO(Student student3,String format) throws Exception {
        DaoStuHandle.info();
        InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis_config.xml");

        //获取一个工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //通过工厂获取一个对象
        SqlSession sqlSession = build.openSession();

        int update= sqlSession.update("StudentMapper.insertData",student3);
        sqlSession.commit();
        if (update!=0){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    public boolean delStuDAO(int stuId) throws Exception {
         DaoStuHandle.info();
        InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis_config.xml");

        //获取一个工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //通过工厂获取一个对象
        SqlSession sqlSession = build.openSession();
        int update= sqlSession.update("StudentMapper.deleteData",stuId);
        sqlSession.commit();
      if (update!=0){
          return true;
      }else {
          return false;
      }

    }
    public boolean updateStuDAO(Student student,int id) throws Exception {
        DaoStuHandle.info();
        InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis_config.xml");

        //获取一个工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //通过工厂获取一个对象
        SqlSession sqlSession = build.openSession();
        student.setId(id);
        int update= sqlSession.update("StudentMapper.updateData",student);
        sqlSession.commit();
        if (update!=0){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
        return false;

    }
    public void searchStuDAO(Student[] stus, Integer len, Integer a) {
        System.out.println("所查找的学生信息是： " + stus[a]);
    }
    }